﻿using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using Core.Domain.Contracts;
using Core.Domain.Events;

namespace Core.Domain;

/// <summary>
/// 基础实体
/// </summary>
/// <typeparam name="TId"></typeparam>
public abstract class BaseEntity<TId> : IEntity<TId>
{
    /// <summary>
    /// 唯一标识
    /// </summary>
    public TId Id { get; set; } = default!;

    /// <summary>
    /// 设置排序
    /// </summary>
    public virtual int? Sort { get; set; }

    /// <summary>
    /// 设置描述
    /// </summary>
    public virtual string? Description { get; set; }

    /// <summary>
    /// 设置启用/禁用
    /// </summary>
    public virtual bool? Enabled { get; set; }

    [NotMapped]
    public Collection<DomainEvent> DomainEvents { get; } = new Collection<DomainEvent>();
    public void QueueDomainEvent(DomainEvent @event)
    {
        if (!DomainEvents.Contains(@event))
            DomainEvents.Add(@event);
    }
}

public abstract class BaseEntity : BaseEntity<Guid>
{
    protected BaseEntity() => Id = Guid.CreateVersion7();
}
